//
//  kW3dcycleCell.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/5/25.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "kW3dcycleCell.h"
#import "kWHomeModel.h"
#import "kWCommonModel.h"
#import "iCarousel.h"
#import "kWHomeModel.h"

@interface kW3dcycleCell ()<iCarouselDelegate,iCarouselDataSource>

@property (nonatomic,strong) iCarousel             *iCarousel;


@end

@implementation kW3dcycleCell
//

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self buildSubViews];
    }
    return self;
}

- (void)buildSubViews
{
    [self.contentView addSubview:self.iCarousel];
    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    WeakSelf(self);
    [self.iCarousel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakself);
        make.width.equalTo(weakself);
        make.height.mas_equalTo(weakself.Height);
    }];
}


- (void)setData:(NSArray<kWHomeModel *> *)data
{
    _data = data;
    [self.iCarousel reloadData];
}

#pragma mark - iCarousel代理

-(NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
    return self.data.count > 0 ? self.data.count : 0;
}

-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    if (view == nil) {
        
        CGFloat viewW = WIDTH/3*2;
        CGFloat viewH = HEIGHT * 0.35 *0.9;
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,viewW, viewH)];
    }
    
    [self.iCarousel scrollToItemAtIndex:1 animated:false];
    
    kWHomeModel *model = [[kWHomeModel alloc] initWithDictionary:(NSDictionary *)self.data[index] error:nil];

    [((UIImageView *)view) sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    return view;
}


-(CATransform3D)carousel:(iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform{
    static CGFloat max_sacle = 1.0f;
    static CGFloat min_scale = 0.7f;
    if (offset <= 1 && offset >= -1) {
        float tempScale = offset < 0 ? 1+offset : 1-offset;
        float slope = (max_sacle - min_scale) / 1;
        
        CGFloat scale = min_scale + slope*tempScale;
        transform = CATransform3DScale(transform, scale, scale, 1);
    }else{
        transform = CATransform3DScale(transform, min_scale, min_scale, 1);
    }
    
    return CATransform3DTranslate(transform, offset * self.iCarousel.itemWidth * 1.3, 0.0, 0.0);
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
    
    kWHomeModel *model = [[kWHomeModel alloc] initWithDictionary:(NSDictionary *)self.data[index] error:nil];
    if (self.threeDBlock) {
        self.threeDBlock(model);
    }
}

- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{

}


#pragma mark lazy
-(iCarousel *)iCarousel{
    if (_iCarousel == nil) {
        _iCarousel = [[iCarousel alloc] init];
        _iCarousel.delegate = self;
        _iCarousel.dataSource = self;
        _iCarousel.bounces = NO;
        _iCarousel.pagingEnabled = YES;
        _iCarousel.type = iCarouselTypeCustom;
    }
    return _iCarousel;
}


@end
